package com.retail.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.retail.common.core.domain.Result;
import com.retail.common.core.utils.StringUtils;
import com.retail.order.domain.OrderSku;
import com.retail.order.domain.TTake;
import com.retail.order.domain.Take;
import com.retail.order.mapper.OrderSkuMapper;
import com.retail.order.service.OrderSkuService;
import com.retail.order.util.SnowFlakeUtil;
import com.retail.order.vo.OrderRequest;
import com.retail.order.vo.SkuNumReq;
import com.retail.shopping.common.domain.response.CartListResponse;
import net.bytebuddy.utility.nullability.AlwaysNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class OrderSkuServiceImpl implements OrderSkuService {
    @Autowired
    OrderSkuMapper orderSkuMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    private final String address="CART_USER_ID_";

    private final String redisKey = "SHOPPING_CART:";
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderRequest getOrderId(OrderRequest orderRequest) {
        //判断redis中有没有这条数据 有就对这个状态进行更改
        String key = redisKey+address+orderRequest.getUserId ();
        BoundHashOperations<String, Object, Object> cartMap = redisTemplate.boundHashOps (key);
        for (SkuNumReq skuNumReq : orderRequest.getList ()) {
            String cart = (String)cartMap.get (skuNumReq.getSkuId ()+"");
            if (cart!=null){
                CartListResponse cartListResponse = JSON.parseObject (cart, CartListResponse.class);
                cartListResponse.setCartStatus (2);
                cartMap.put (skuNumReq.getSkuId ()+"", JSON.toJSONString (cartListResponse));
            }
        }
        SnowFlakeUtil snowFlakeUtil = new SnowFlakeUtil();
        long id = snowFlakeUtil.nextId();
        Date date = SnowFlakeUtil.getTimeBySnowFlakeId(id);
        long time = date.getTime();
        orderRequest.setOrderCode(String.valueOf(time));
        orderSkuMapper.insertOrder(orderRequest);
        orderSkuMapper.insertOrderSku (orderRequest);
        return orderRequest;

//        if (i<=0){
//            return Result.error("下单错误");
//        }else {
//            orderSkuMapper.insertOrderSku(orderRequest);
//            return Result.success(orderRequest,"下单成功");
//        }
    }

    @Override
    public List<Take> getUserTake(Integer userId) {
        return orderSkuMapper.selectUserTake(userId);
    }

    @Override
    public Result getOrderRespone(String uuid) {
        if (Boolean.TRUE.equals (redisTemplate.hasKey (uuid))){
            String s = redisTemplate.opsForValue ().get (uuid);
            if (StringUtils.isNotBlank (s)) {
                OrderRequest orderResponse = JSON.parseObject (s, OrderRequest.class);
                return Result.success (orderResponse);
            }
        }
        return Result.error ("暂无数据");
    }

    @Override
    public List<OrderSku> getCartOrderList(String orderId) {
        return orderSkuMapper.getCartOrderList(orderId);
    }
}
